package com.mixsmart.commonweb.service.impl;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.mixsmart.commonweb.IWebConstant;
import com.mixsmart.commonweb.bean.UserInfo;
import com.mixsmart.commonweb.bean.entity.TCOrg;
import com.mixsmart.commonweb.bean.entity.TCUser;
import com.mixsmart.commonweb.dao.PKStringDao;
import com.mixsmart.commonweb.enums.OrgType;
import com.mixsmart.commonweb.service.IUserService;
import com.mixsmart.core.bean.MixResponse;
import com.mixsmart.enums.YesNoType;
import com.mixsmart.security.SecurityUtils;
import com.mixsmart.spellsql.utils.Cnd;
import com.mixsmart.spellsql.utils.Exp;
import com.mixsmart.utils.StringUtils;

/**
 * 用户服务--实现类
 * @author lmq
 * @version 1.0
 * @since JDK版本大于等于1.6
 * 2016年1月26日
 */
@Service
public class UserService extends SortNumService<TCUser> implements IUserService {

	private static final Logger logger = Logger.getLogger(UserService.class);
	
	@Autowired
	private ServiceContext servContext;
	
	@Override
	public MixResponse<String> changePassword(String userId, String oldPwd, String newPwd) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	@Transactional(propagation=Propagation.SUPPORTS)
	public MixResponse<UserInfo> checkLogin(String username, String password) {
		MixResponse<UserInfo> mixResp = new MixResponse<UserInfo>();
		String msg = "用户名或密码为空";
		mixResp.setMsg("请输入用户名或密码");
		if(StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(password)) {
			mixResp.setMsg("用户名或密码输入错误");
			TCUser user = super.find(Cnd.where(Exp.eq("username", username).and(Exp.eq("state", YesNoType.YES.getIndex()))));
			if(null != user) {
					String md5Pwd = SecurityUtils.md5(password);
					if(md5Pwd.equals(user.getPassword())) {
						msg = "用户登录成功";
						UserInfo userInfo = new UserInfo();
						userInfo.setUserId(user.getId());
						userInfo.setUsername(user.getUsername());
						userInfo.setFullName(user.getFullName());
						userInfo.setDepartId(user.getOrgId());
						TCOrg org = null ;
						String orgId = user.getOrgId();
						//查找该用户所属的机构
						boolean isSearchOrg = true;
						while(isSearchOrg) {
							org = servContext.getOrgServ().find(orgId);
							if(org == null || org.getType().equals(OrgType.ORG.getValue())) {
								isSearchOrg = false;
							} else {
								orgId = org.getParentId();
							}
						}
						if(null != org) 
							userInfo.setOrgId(org.getId());
						else 
							userInfo.setOrgId(IWebConstant.TREE_ROOT_ID);
						//待实现；实现内容：获取用户所在的部门名称；用户拥有的角色ID等
						mixResp.setResult(IWebConstant.OP_SUCCESS);
						mixResp.setData(userInfo);
					} else {
						msg = "密码输入错误或用户禁用，用户名为["+username+"]---";
					}
			} else {
					msg = "用户名输入错误--用户输入的用户名为["+username+"]---";
			}
		}
		logger.info(msg);
		mixResp.setMsg(msg);
		return mixResp;
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
	public boolean save(TCUser t) {
		if(null != t && StringUtils.isNotEmpty(t.getPassword())) {
			t.setPassword(SecurityUtils.md5(t.getPassword()));
		}
		return super.save(t);
	}

	@Override
	public PKStringDao<TCUser> getDao() {
		return (PKStringDao<TCUser>) dao;
	}
	
}
